#include "cmd_view.h"
#include "cmd_executor.h"
#include "cmd_parser.h"
#include <cassert>

using namespace vpm::cmo;

CmdView::CmdView(const std::string & viewName):m_viewName(viewName)
{
	m_rootNode.reset(new CmdNode(CmdNode::Root,"")); 
	m_rootNode->index = 0;
	m_lastNode  = m_rootNode; 
	m_viewPrompt = ">";			
}


//parse phrase 
void CmdView::add_keyword(const std::string & key, CmdNode::NodeType type,CmdExecutorPtr pExecutor)
{	
	CmdNodePtr pNode = m_lastNode->find_keyword(key);
	if (!pNode)
	{	
		pNode.reset(new CmdNode(type,key,"",m_lastNode));	
		pNode->index = m_lastNode->index+1;
		m_lastNode->children.insert(pNode);			
	}
	m_lastNode = pNode; 		
}	



//must the last word of the command. can add action here.
//parse phrase
void CmdView::add_action(CmdExecutorPtr pExecutor)
{
	//std::cout <<"add Action here "<<std::endl;
	m_lastNode->executor = pExecutor;
	m_lastNode->command  = pExecutor->command();
}

void CmdView::add_command(const std::string & cmd, CmdExecutorPtr pExecutor)
{
	pExecutor->command(cmd);
	m_cmdExecutors.insert(std::make_pair(cmd,pExecutor));
	CmdParser cmdParser(*this,pExecutor);
	parse_info<> info = parse(cmd.c_str(),cmdParser);
}

void CmdView::add_command(CmdExecutorPtr pExecutor)
{
	assert(!pExecutor->command().empty());
	m_cmdExecutors.insert(std::make_pair(pExecutor->command(),pExecutor));
	CmdParser cmdParser(*this,pExecutor);
	parse_info<> info = parse(pExecutor->command().c_str(),cmdParser);	
}

